package api

import (
	"errors"
	"fmt"
	"github.com/labstack/echo/v4"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	protobuf2 "longmen/server/pkg/protobuf"
	"strings"
)

// @Summary 查询首页中有料列表
// @Description 查询首页中有料列表
// @Accept  json
// @Produce  json
// @Tags expert
// @Success 200 {object} api_models.ExpertComp "result"
// @Router /api/expert/getExpertCompMenu [POST]
func (base *Controller) GetExpertCompMenu(c echo.Context) error {
	expertComps := make([]*api_models.ExpertComp, 0)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	if compList, err := grpc_client.ExpertServerClient.GetExpertCompMenu(ctx, &protobuf2.Empty{}); err == nil {
		for _, val := range compList.List {
			e := &api_models.ExpertComp{
				SportName: val.SportName,
				SportId:   val.SportId,
			}
			if len(val.Comps) > 0 {
				e.Comps = strings.Split(val.Comps, ",")
			} else {
				e.Comps = make([]string, 0)
			}
			if len(val.CompShortZhs) > 0 {
				e.CompShortZhs = strings.Split(val.CompShortZhs, ",")
			} else {
				e.CompShortZhs = make([]string, 0)
			}
			expertComps = append(expertComps, e)
		}

	}
	return util.BuildSuccess(c, expertComps, "")
}

// @Summary 查询首页中有料列表
// @Description 查询首页中有料列表
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertPlanListReq true "body"
// @Success 200 {object} protobuf.ExpertPlanListResp "result"
// @Router /api/expert/getExpertPlanList [POST]
func (base *Controller) GetExpertPlanList(c echo.Context) error {
	/*_, ok := c.Get(util.HTTP_KEY_UID).(int)
	if !ok {
		return common.BuildFail(c, util.RPC_FAILED, util.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}*/
	e := &protobuf2.ExpertPlanListReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	/*if e.SportId <= 0 {
		return common.BuildFail(c, util.RPC_FAILED, "sportId不能为空", errors.New("sportId不能为空"))
	}*/
	e.Page = int32(string2.GetDefaultNum(int(e.Page), _const.LIST_PAGE_DEFAULT))
	e.Size = int32(string2.GetDefaultNum(int(e.Size), _const.LIST_SIZE_DEFAULT))
	if err := _const.Validate.Struct(e); err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetExpertPlanList(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 查询赛事详情有料列表
// @Description 查询赛事详情有料列表
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertFromMatchReq true "body"
// @Success 200 {object} protobuf.ExpertPlanListResp "result"
// @Router /api/expert/getExpertFromMatchList [POST]
func (base *Controller) GetExpertFromMatchList(c echo.Context) error {
	/*_, ok := c.Get(util.HTTP_KEY_UID).(int)
	if !ok {
		return common.BuildFail(c, util.RPC_FAILED, util.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆")))
	}*/
	e := &protobuf2.ExpertFromMatchReq{}

	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if e.MatchId <= 0 || e.SportId <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, "sportId不能为空或MatchId不能为空", errors.New("sportId不能为空或MatchId不能为空"))
	}
	e.Page = int32(string2.GetDefaultNum(int(e.Page), _const.LIST_PAGE_DEFAULT))
	e.Size = int32(string2.GetDefaultNum(int(e.Size), _const.LIST_SIZE_DEFAULT))
	if err := _const.Validate.Struct(e); err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetExpertFromMatch(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

/**
根据有料ID查询有料信息
*/

// @Summary 根据有料ID查询有料信息
// @Description 根据有料ID查询有料信息
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertPlanReq true "body"
// @Success 200 {object} protobuf.ItemExpertPlan "result"
// @Router /api/expert/getExpertPlan [POST]
func (base *Controller) GetExpertPlan(c echo.Context) error {
	e := &protobuf2.ExpertPlanReq{}
	//如果会员有登入,加入会员ID
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if ok {
		e.UserId = int64(uid)
	}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(e); err != nil {
		return util.BadParam(c, err)
	}
	if e.Id <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("有料ID不能为空!")))
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetExpertPlan(ctx, e)
	if resp == nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.DATA_NO_FOUND, err)
	}
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

/**
根据有料ID支付
*/

// @Summary 根据有料ID支付
// @Description 根据有料ID支付
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertPlanFeeReq true "body"
// @Success 200 {object} protobuf.ExpertPlanFeeResp "result"
// @Router /api/expert/payExpertPlanFee [POST]
func (base *Controller) PayExpertPlanFee(c echo.Context) error {
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok || uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆!")))
	}
	e := &protobuf2.ExpertPlanFeeReq{}

	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(e); err != nil {
		return util.BadParam(c, err)
	}
	if e.ExpertPlanId <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("有料ID不能为空!")))
	}
	//加入会员ID
	e.UserId = int64(uid)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.PayExpertPlanFee(ctx, e)
	global.Log.Debugf("resp:%+v,err:%v", resp, err)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	if resp.Code == _const.RPC_BALANCE_NOT_ENOUGH {
		return util.BuildFail(c, _const.RPC_FAILED, global.Lang.T("insufficient_balance"), errors.New(global.Lang.T("insufficient_balance")))
	}
	if resp.Code == _const.RPC_REPEAT_PAY {
		return util.BuildFail(c, _const.RPC_FAILED, "重复支付,已支付.", errors.New("重复支付,已支付."))
	}
	if resp.Code != _const.RPC_SUCCEED {
		return util.BuildFail(c, _const.RPC_FAILED, "系统繁忙.", errors.New(resp.GetMessage()))
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

//v2 开始处

// @Summary 鹰眼搜索-热门搜索(v2) 特别说明：前端显示使用label,传入后端的值请使用comp的值 调用GetExpertPlanList
// @Description 鹰眼搜索-热门搜索(v2) 特别说明：前端显示使用label,传入后端的值请使用comp的值 调用GetExpertPlanList
// @Accept  json
// @Produce  json
// @Tags expert
// @Success 200 {object} protobuf.HotCompLabelResp "result"
// @Router /api/expert/getHotComp [POST]
func (base *Controller) GetHotComp(c echo.Context) error {
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetHotComp(ctx, &protobuf2.Empty{})
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary 鹰眼搜索-赛事搜索(v2) 模糊查询返回赛事列表 调用此接口GetExpertPlanList 显示相关赛事的鹰眼信息
// @Description 鹰眼搜索-赛事搜索(v2) 糊查询返回赛事列表 调用此接口GetExpertPlanList 显示相关赛事的鹰眼信息
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.MatchLikeSearchReq true "body"
// @Success 200 {object} protobuf.MatchLikeSearchResp "result"
// @Router /api/expert/getMatchLikeSearch [POST]
func (base *Controller) GetMatchLikeSearch(c echo.Context) error {
	e := &protobuf2.MatchLikeSearchReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetMatchLikeSearch(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary 专家信息
// @Description 专家信息
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertSearchCond true "body"
// @Success 200 {object} protobuf.ExpertInfoResp "result"
// @Router /api/expert/getExpertList [POST]
func (base *Controller) GetExpertList(c echo.Context) error {
	uid, _ := c.Get(_const.HTTP_KEY_UID).(int)
	e := &protobuf2.ExpertSearchCond{}

	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if e.Cond == _const.EXPERT_ATTENTION && uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("会员未有登陆!"))
	}
	e.UserId = int64(uid)
	e.Page = int32(string2.GetDefaultNum(int(e.Page), _const.LIST_PAGE_DEFAULT))
	e.Size = int32(string2.GetDefaultNum(int(e.Size), _const.LIST_SIZE_DEFAULT))
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetExpertList(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary 根据ID查询专家信息(v2)
// @Description 根据ID查询专家信息(v2)
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.ExpertByIdReq true "body"
// @Success 200 {object} protobuf.ExpertInfo "result"
// @Router /api/expert/getExpertById [POST]
func (base *Controller) GetExpertById(c echo.Context) error {
	e := &protobuf2.ExpertByIdReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if e.ExpertId <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("专家ID不能为空!"))
	}
	uid, _ := c.Get(_const.HTTP_KEY_UID).(int)
	e.UserId = int32(uid)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.GetExpertById(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	return util.BuildSuccess(c, resp, _const.EMPTY_STR)
}

// @Summary 会员关注/取消(v2)
// @Description 会员关注/取消(v2)
// @Accept  json
// @Produce  json
// @Tags expert
// @Param   sendBody body protobuf.AttentionExpertReq true "body"
// @Success 200 {object} protobuf.AttentionExpertResp "result"
// @Router /api/expert/attentionExpert [POST]
func (base *Controller) AttentionExpert(c echo.Context) error {
	uid, ok := c.Get(_const.HTTP_KEY_UID).(int)
	if !ok || uid <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New(fmt.Sprintf("请会员先登陆!")))
	}
	e := &protobuf2.AttentionExpertReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), e); err != nil {
		return util.BadParam(c, err)
	}
	if e.ExpertId <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("专家ID不能为空!"))
	}
	if e.Attention <= 0 {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, errors.New("操作类型不能为空!"))
	}
	e.UserId = int64(uid)
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.ExpertServerClient.AttentionExpert(ctx, e)
	if err != nil {
		return util.BuildFail(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}
	/*if resp.Code == util.RPC_FAILED {
		return common.BuildFail(c, util.RPC_FAILED, "操作失败", errors.New("操作失败"))
	}*/
	return util.BuildSuccess(c, resp, "操作成功")
}
